package com.java.test.agent.module.apply;

import com.java.test.agent.module.apply.dto.AgentApplyQueryDTO;
import com.java.test.agent.module.apply.entity.AgentApplyEntity;
import com.java.test.agent.module.feescale.entity.AgentFeeScaleEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 代理申请DAO
 *
 * @author lijie1
 * @date 2020/6/30 0030
 */
@Component
public class AgentApplyDao {

    private JdbcTemplate jdbcTemplate;
    @Autowired
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    /**
     * 查询
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public List<AgentApplyEntity> selectByList(AgentApplyQueryDTO queryDTO) {
        StringBuffer sql = new StringBuffer("select * from agent_apply t where  1 = 1 ");
        List<AgentApplyEntity> info = new ArrayList<AgentApplyEntity>();;
        List<Object> param = new ArrayList();
        try {
            if(queryDTO.getStatus()!=null && !"".equals(queryDTO.getStatus())){
                sql.append(" and t.status in("+queryDTO.getStatus()+")");
            }
            info= jdbcTemplate.query(sql.toString(), param.toArray(), new AgentApplyEntity());
        }catch (EmptyResultDataAccessException e){
            return null;
        }
        return info;
    }

    /**
     * 查询
     * @author lijie1
     * @date 2020/6/30 0030
     */
    public List<AgentApplyEntity> getSelectByList(AgentApplyQueryDTO queryDTO) {
        StringBuffer sql = new StringBuffer("select * from agent_apply t where  1 = 1 ");
        List<AgentApplyEntity> info = new ArrayList<AgentApplyEntity>();;
        List<Object> param = new ArrayList();
        try {
            if(queryDTO.getStatus()!=null && !"".equals(queryDTO.getStatus())){
                sql.append(" and t.status = ?");
                param.add(queryDTO.getStatus());
            }
            if(queryDTO.getAccount()!=null && !"".equals(queryDTO.getAccount())){
                sql.append(" and t.account like '%"+queryDTO.getAccount()+"%' ");
            }
            if(queryDTO.getApplyTime()!=null && !"".equals(queryDTO.getApplyTime())){
                sql.append(" and DATE_FORMAT(t.createTime,'%Y-%m-%d %H:%i:%s') >= STR_TO_DATE( ? ,'%Y-%m-%d %H:%i:%s') ");
                param.add(queryDTO.getApplyTime());
            }
            sql.append(" limit ? , ?");
            int startSize = (queryDTO.getPageNum()*queryDTO.getPageSize())-queryDTO.getPageSize();
            int endSize = (queryDTO.getPageNum()*queryDTO.getPageSize());
            param.add(startSize);
            param.add(endSize);
            info= jdbcTemplate.query(sql.toString(), param.toArray(), new AgentApplyEntity());
        }catch (EmptyResultDataAccessException e){
            return null;
        }
        return info;
    }

    /**
     * 查询当前id是否存在
     * @author lijie1
     * @date 2020/7/1 0001
     */
    public List<AgentApplyEntity> getByIds(int status,String ids) {

        String sql = "select * from agent_apply t where t.status=? and t.id in("+ids+")";

        List<AgentApplyEntity> list = new ArrayList<>();
        try {
            list = jdbcTemplate.query(sql,new AgentApplyEntity(),status);
        }catch (EmptyResultDataAccessException e){
            return null;
        }
        return list;
    }

    /**
     * 修改
     * @author lijie1
     * @date 2020/7/1 0001
     */
    public void updateById(int status,String ids) {
        String sql = "update agent_apply " +
                "set  status = ? " +
                "where id in("+ids+")";

        jdbcTemplate.update(sql,status);
    }

    /**
     * 审核通过添加代理商信息
     * @author lijie1
     * @date 2020/7/1 0001
     */
    public void insertAgentInfo(AgentApplyEntity entity, AgentFeeScaleEntity en) {
        StringBuffer sql = new StringBuffer("insert into agent_info(account,name,email,phone,createTime,feescale,brokeragescale) ");
        sql.append(" values(?,?,?,?,?,?,?) ");

        jdbcTemplate.update(sql.toString(),entity.getAccount(),entity.getName(),entity.getEmail(),entity.getPhone(),new Date(),en.getFeeScale(),en.getBrokerageScale());
    }
}
